from PyQt4.QtGui import QWidget
from PyQt4 import QtCore,QtGui
from KCharts.KPlotItem.KSingleChartPlot import KSingleChartPlot
from KCharts.KPlotItem.KChartPriceDisplayItem import KChartPriceDisplayItem
from KCharts.KDisplayItem.KChartBaseDisplayItem import KChartBaseDisplayItem
import pyqtgraph as pg
from pyqtgraph.dockarea import *
import numpy as np
class KSingleChartCurvePlot(KSingleChartPlot):
	'''
	subclass of KSingleChartPlot, is responsible for render curve information(such as volumn) on the plot.
	'''
	PeriodChanged=QtCore.pyqtSignal(str)

	def __init__(self, parent,stockobject):
		super(KSingleChartCurvePlot, self).__init__(parent,stockobject)



		item = KChartCurveDisplayItem(stockobject)
		self.plot.clearPlots()
		self.plot.addItem(item)

class KChartCurveDisplayItem(KChartBaseDisplayItem):
	'''dieplay item specially for price'''
	def __init__(self, stockobject):
		super(KChartCurveDisplayItem,self).__init__(stockobject)

		
	def PrepareRenderPicture(self):
		## pre-computing a QPicture object allows paint() to run much more quickly, 
		## rather than re-drawing the shapes every time.
		priceblocks=self.m_stock.CurWindowData('Price')
		XCordinates=self.m_stock.CurWindowData('Index')

		self.picture = QtGui.QPicture()
		p = QtGui.QPainter(self.picture)
		
		width=1.0/3.0
		for index in range(0,len(priceblocks)):
			(openp,highp,lowp,closep,vol)=priceblocks[index]
			xcord=XCordinates[index]
			if openp > closep:
				p.setPen(pg.mkPen('r'))	
				
			else:
				p.setPen(pg.mkPen('g'))			
			if vol>0:	# draw only when vol not empty
				p.drawLine(QtCore.QPointF(xcord, 0), QtCore.QPointF(xcord, vol))		
		p.end()	